/**
 * 实际费用结算维护
 * @author miaoyl
 * @date 2019/01/16
 */
define(function () {

    var self,
        doc = document,
        app = win.app,
        layui = win.layui,
        element,
        table,
        table_gwfd = {},
        cols = [[
            {field: 'ID', type: 'checkbox', title: '选择', fixed: 'left'}
            , {field: 'GWFDDOCNO', width: 140, title: '实际费用计算单号', fixed: 'left', sort: true}
            , {field: 'GWFDSEQ', width: 80, title: '项次',sort:'default'}
            , {field: 'GWFD001', width: 80, title: '年度',searchOpts: {type: 'year'},search:'default'}
            , {field: 'GWFD002', width: 80, title: '月份',searchOpts: {type: 'MM'},search:'default'}
            , {field: 'GWFD003_NAME', width: 100, title: '区域',search: 'default',searchOpts:{icon:true}}
            , {field: 'GWFD004_NAME', width: 120, title: '客户',search: 'default',searchOpts:{icon:true}}
            , {field: 'GWFD005_NAME', width: 120, title: '品类',search: 'default',searchOpts:{icon:true}}
            , {field: 'GWFD006_NAME', width: 120, title: '业务组织',search: 'default',searchOpts:{icon:true}}
            , {field: 'GWFD007_NAME', width: 120, title: '费用'}
            , {field: 'GWFD008', width: 120, title: '费用来源',
                filterDatas: app.datas_('crm_gwft300_gwfd008')
            }
            , {field: 'GWFD100_NAME', width: 120, title: '币别'}
            , {field: 'GWFD101', width: 120, title: '汇率'}
            , {field: 'GWFD102', width: 140, title: '实际销售原币金额'}
            , {field: 'GWFD103', width: 140, title: '实际销售本币金额'}
            , {field: 'GWFD104', width: 140, title: '区域费率'}
            , {field: 'GWFD105', width: 140, title: '总部费率'}
            , {field: 'GWFD106', width: 140, title: '区域实际费用原币金额'}
            , {field: 'GWFD107', width: 140, title: '区域实际费用本币金额'}
            , {field: 'GWFD108', width: 140, title: '总部实际费用原币金额'}
            , {field: 'GWFD109', width: 140, title: '总部实际费用本币金额'}
            , {
                field: 'GWFDACTI', width: 80, title: '审核否',
                filterDatas: app.datas_('crm_gwei005_gwecconf')
            },
            {
                width: 60,
                title: '',
                toolbar: '#script-gwft300setting',
                fixed: 'right',
                align: 'center',
                unresize: true
            }
        ]],
        gwft300SettingAction,
        moduleName = "实际费用结算"
    ;

    var Gwft300 = app.Module.extend({
        init: function () {
            self = this;
            self._super({
                _tag: 'gwft300',
                _searchs: [{
                    id: 'gwft300', tableId: 'gwft300_table', datas: cols[0]
                }],
                _loadTplsFull: [{
                    dom: 'gwft300_edit', tpl: '/gwft300_edit'
                },{
                    dom: 'common_open_window', tpl: 'common/_open_window'
                },{
                    dom: 'gwft300_auto', tpl: '/gwft300_auto' //2
                }]
            }, win);
        },

        initTable: function () {
            layui.use(['table'], function () {
                table = layui.table;
                table.render({
                    elem: app.getByid(doc, 'gwft300_table'),
                    url: g_ctxPath + 'crm/gwft300/getGwfdList',
                    exportsUrl: g_ctxPath + 'crm/gwft300/exportsData',
                    exportsName: moduleName + '数据',
                    title: moduleName + '信息表',
                    method: 'post',
                    id: 'gwft300_table',
                    size: _layuiConfig.ui_size,
                    autoSort: false, //禁用前端自动排序
                    toolbar: '#table-toolbar',
                    defaultToolbar: ['filter', 'exports'],
                    height: 'full',
                    cols: cols,
                    loading: true,
                    page: true,
                    where: '',
                    done: function (res, curr, count) {

                    }
                });

                layui.laytpl(app.getByid(doc, 'script-gwft300setting-action').innerHTML).render({}, function (string) {
                    gwft300SettingAction = string;
                });

                var tipsIndex = null;

                table.on('tool(gwft300_table)', function (obj) {

                    layer.tips(gwft300SettingAction, obj.td, {
                        tips: [3, '']
                        , time: 3000
                        , anim: 500
                        , maxWidth: 120
                        , skin: 'layui-table-tips'
                        , success: function (layero, index) {
                            layero.find('li').on('click', function () {
                                switch ($(this).attr('inx')) {
                                    case '0':
                                        self._openTplFull({
                                            tag: 'gwft300_edit',
                                            tagData: {
                                                obj: obj,
                                                moduleName: moduleName
                                            },
                                            index: 0,
                                            openOpts: {
                                                title: moduleName + '编辑',
                                                maxmin: true
                                            }
                                        });
                                        break;
                                    case '1':
                                        self.del(obj);
                                        break;
                                }
                                layer.close(index);
                            });
                        }
                    });
                });

                //监听行双击事件
                table.on('rowDouble(gwft300_table)', function (obj) {
                    self._openTplFull({
                        tag: 'gwft300_edit',
                        tagData: {
                            obj: obj,
                            is_check:true,
                            moduleName: moduleName
                        },
                        index: 0,
                        openOpts: {
                            title: moduleName + '编辑',
                            maxmin: true
                        }
                    });
                });

                //监听事件
                table.on('toolbar(gwft300_table)', function (obj) {
                    switch (obj.event) {
                        case 'add':
                            self._openTplFull({
                                tag: 'gwft300_edit',
                                tagData: {
                                    is_add: true,
                                    moduleName: moduleName
                                },
                                openOpts: {
                                    title: moduleName + '新增',
                                    maxmin: true
                                },
                                index: 0
                            });
                            break;
                        case 'del':
                            self.deleteDataBatch();
                            break;
                        case 'check':
                            self.check();
                            break;
                        case 'unCheck':
                            self.unCheck();
                            break;
                        case 'batch':
                            self._openTplFull({
                                tag: 'gwft300_auto',
                                tagData: {
                                    moduleName: moduleName
                                },
                                openOpts: {
                                    title: moduleName + '批量生成',
                                    area: ['500px','300px']
                                },
                                index: 2
                            });
                            break;
                    }
                });

                $(doc).on('click', function () {
                    tipsIndex && layer.close(tipsIndex);
                });

            });
        },

        reloadTableCurr: function () {
            self._reloadTable({id: 'gwft300_table', where: table_gwfd});
        },

        del: function (obj) {
            app.confirm({
                msg: '是否删除选择的信息？',
                ok: function () {
                    app.ajax({
                        xurl: 'crm/gwft300/del',
                        xdata: obj.data,
                        loading: true,
                        okmsg: moduleName + '信息删除成功',
                        cb: function (result) {
                            if (result.ok) {
                                self.reloadTableCurr();
                            }
                        }
                    });
                }
            });

        },

        deleteDataBatch: function () {
            var flag = true;
            var checkStatus = table.checkStatus('gwft300_table');
            if (checkStatus.data.length > 0) {
                var _xdata = [];
                var _choose = [];
                checkStatus.data.forEach(function (v) {
                    var _temp = app.fields_('GWFD_T');
                    if (v.GWFDACTI != 'N') {
                        app.showWarning("只有状态为N：未审核的费用计算单才可删除！");
                        flag = false;
                        return;
                    } else {
                        if(_choose.indexOf(v.GWFDDOCNO) == -1){
                            _choose.push(v.GWFDDOCNO);
                            _temp.GWFDENT = v.GWFDENT;
                            _temp.GWFDDOCNO = v.GWFDDOCNO;
                            _xdata.push(_temp);
                        }

                    }
                });
            } else {
                app.showWarning("请先选择单据！");
                return;
            }
            if (flag) {
                app.ajax({
                    xurl: 'crm/gwft300/deleteDataBatch',
                    xdata: _xdata,
                    loading: true,
                    toJsonStr: true,
                    confirm: {msg: "是否确认删除?"},
                    okmsg: moduleName + '信息删除成功',
                    cb: function (result) {
                        if (result.ok) {
                            self.reloadTableCurr();
                        }
                    }
                });
            }
        },

        check: function () {
            var flag = true;
            var checkStatus = table.checkStatus('gwft300_table');
            if (checkStatus.data.length > 0) {
                var _xdata = [];
                var _choose = [];
                checkStatus.data.forEach(function (v) {
                    var _temp = app.fields_('GWFD_T');
                    if (v.GWFDACTI != 'N') {
                        app.showWarning("只有状态为N：未审核的费用计算单可以审核！");
                        flag = false;
                        return;
                    } else {
                        if(_choose.indexOf(v.GWFDDOCNO) == -1){
                            _choose.push(v.GWFDDOCNO);
                            _temp.GWFDENT = v.GWFDENT;
                            _temp.GWFDDOCNO = v.GWFDDOCNO;
                            _temp.GWFDACTI = 'Y';
                            _xdata.push(_temp);
                        }
                    }
                });
            }else {
                app.showWarning("请先选择单据！");
                return;
            }
            if(flag){
                app.ajax({
                    xurl: 'crm/gwft300/updateStatus',
                    xdata: _xdata,
                    loading: true,
                    toJsonStr: true,
                    confirm: {msg: "是否确认审核?"},
                    okmsg: moduleName + '信息审核成功',
                    cb: function (result) {
                        if (result.ok) {
                            self.reloadTableCurr();
                        }
                    }
                });
            }
        },

        unCheck: function () {
            var flag = true;
            var checkStatus = table.checkStatus('gwft300_table');
            if (checkStatus.data.length > 0) {
                var _xdata = [];
                var _choose = [];
                checkStatus.data.forEach(function (v) {
                    var _temp = app.fields_('GWFD_T');
                    if (v.GWFDACTI != 'Y') {
                        app.showWarning("只有状态为Y：已审核的费用计算单可以取消审核！");
                        flag = false;
                        return;
                    } else {
                        if(_choose.indexOf(v.GWFDDOCNO) == -1){
                            _choose.push(v.GWFDDOCNO);
                            _temp.GWFDENT = v.GWFDENT;
                            _temp.GWFDDOCNO = v.GWFDDOCNO;
                            _temp.GWFDACTI = 'N';
                            _xdata.push(_temp);
                        }
                    }
                });
            }else {
                app.showWarning("请先选择单据！");
                return;
            }
            if(flag){
                app.ajax({
                    xurl: 'crm/gwft300/updateStatus',
                    xdata: _xdata,
                    loading: true,
                    toJsonStr: true,
                    confirm: {msg: "是否取消审核?"},
                    okmsg: moduleName + '信息取消审核成功',
                    cb: function (result) {
                        if (result.ok) {
                            self.reloadTableCurr();
                        }
                    }
                });
            }
        },

        _ready: function () {
            this._super({
                vue: [{
                    el: '#page',
                    data: '',
                    searchId: 'gwft300',
                    methods: {
                        handleIconClick: function (field) {
                            var vue = this;
                            if (field == 'GWFD004_NAME') {
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method:'q_gwak001',
                                            columnList: [
                                                {label: '客户编号', prop: 'GWAK001', width: '150'},
                                                {label: '客户简称', prop: 'GWAKL003', width: '150'},
                                                {label: '客户全称', prop: 'GWAKL004', width: '150'}
                                            ]
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.GWFD004_NAME = data.GWAK001;
                                        },
                                    },
                                    index: 1,
                                    openOpts: {
                                        title: '选择客户',
                                        area: ['600px', '470px']
                                    }
                                });
                            }else if (field == 'GWFD003_NAME') {
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method:'q_gwad002',
                                            columnList: [{label: '区域编号', prop: 'GWAD002', width: '150'},
                                                {label: '区域名称', prop: 'GWADL004', width: '150'}]
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.GWFD003_NAME = data.GWAD002;
                                        },
                                    },
                                    index: 1,
                                    openOpts: {
                                        title: '选择区域',
                                        area: ['600px', '470px']
                                    }
                                });
                            }else if (field == 'GWFD005_NAME') {
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method: 'q_gwao001',
                                            columnList: [{label: '系列编号', prop: 'GWAO001', width: '150'},
                                                {label: '系列名称', prop: 'GWAOL003', width: '150'}
                                            ],
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.GWFD005_NAME = data.GWAO001;
                                        },
                                    },
                                    index: 1,
                                    openOpts: {
                                        title: '选择品类',
                                        area: ['600px', '470px']
                                    }
                                });
                            }else if (field == 'GWFD006_NAME') {
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method: 'q_sysorg_id',
                                            columnList: [{label: '组织ID', prop: 'ID', width: '150'},
                                                {label: '组织名称', prop: 'NAME', width: '150'}
                                            ],
                                            searchParam:{
                                                TYPE: '9'
                                            }
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.GWFD006_NAME = data.ID;
                                        },
                                    },
                                    index: 1,
                                    openOpts: {
                                        title: '选择业务组织',
                                        area: ['600px', '470px']
                                    }
                                });
                            }
                        }

                    }
                }]
            });
            self.initTable();
        },
        _lazy: function () {
            this._super();
            layui.use(['table'], function () {
                element = layui.element;
            });
        }
    });

    return new Gwft300();

});